/*
@Time : 2018/11/10 下午2:39 
@Author : baoye
@File : client
@Software: GoLand
@brief: 
*/

package main

import (
	"os"
	"fmt"
	"strconv"
)

type CLI struct {
}

const Usage  = `
	addBlock DATA "add a block"
	printChain "print block Chain"
	createBlockChain ADDRESS "create a blockchian"
	getBalance ADDRESS
	send FROM TO AMOUNT MINER

`

func (cli *CLI)Run()  {
	if len(os.Args) < 2 {
		fmt.Printf(Usage)
		os.Exit(1)
	}

	cmd := os.Args[1]

	switch cmd {
	case "addBlock":
		checkArgs(3)
		//data := os.Args[2]
		//cli.addBlock(data)
	case "printChain":
		cli.PrintChain()
	case "createBlockChain":
		checkArgs(3)
		address := os.Args[2]
		cli.CreateBlockChain(address)
	case "getBalance":
		checkArgs(3)
		address := os.Args[2]
		cli.GetBalance(address)
	case "send":
		checkArgs(6)
		from := os.Args[2]
		to := os.Args[3]
		amount, err:= strconv.ParseFloat(os.Args[4], 64)
		if err != nil {
			fmt.Println("参数错误！")
			os.Exit(1)
		}
		miner := os.Args[5]
		cli.Send(from, to, amount, miner)
	default:
		fmt.Printf(Usage)
	}
}


func checkArgs(count int)  {
	if len(os.Args) != count {
		fmt.Println("参数无效！")
		os.Exit(1)
	}
}